//
//  ZGRPC_Manager.m
//  NewProject
//
//  Created by Apple on 2018/7/10.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZGRPC_Manager.h"
#import "ZKeyChain.h"
#import <GRPCClient/GRPCCall+ChannelArg.h>
#import <Wallet/Wewallet.pbrpc.h>
#import <GRPCClient/GRPCCall+Tests.h>
#import <Wewalletsdk/Wewalletsdk.h>
#import "ZNetManager.h"
//#import <GRPCClient/GRPCCall+ChannelCredentials.h>
#define FACTORY  [ZGRPC_Manager factoryManager]
#define WEWALLET [ZGRPC_Manager walletManager]
static NSString * const kHostAddress = @"we.dapp.cnwan.com:1900";
@implementation ZGRPC_Manager
static WeWallet *wallet;
+ (WeWallet *)walletManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [GRPCCall useInsecureConnectionsForHost:kHostAddress];
        [GRPCCall setUserAgentPrefix:@"Wallet/1.0" forHost:kHostAddress];
//        NSString *caCrtPath = [[NSBundle mainBundle] pathForResource:@"ca" ofType:@"crt"];
//        NSString *ca = [[NSString alloc]initWithContentsOfFile:caCrtPath encoding:NSUTF8StringEncoding error:nil];
//        ca = [ca stringByReplacingOccurrencesOfString:@"-----BEGIN CERTIFICATE-----" withString:@""];
//        ca = [ca stringByReplacingOccurrencesOfString:@"-----END CERTIFICATE-----" withString:@""];
//        NSString *clientKeyPath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"key"];
//        NSString *clientPrivate =[[NSString alloc]initWithContentsOfFile:clientKeyPath encoding:NSUTF8StringEncoding error:nil];
//        clientPrivate = [clientPrivate stringByReplacingOccurrencesOfString:@"-----BEGIN RSA PRIVATE KEY-----" withString:@""];
//        clientPrivate = [clientPrivate stringByReplacingOccurrencesOfString:@"-----END RSA PRIVATE KEY-----" withString:@""];
//
//        NSString *clientCrtPath  = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"crt"];
//        NSString *clientCrtStr = [[NSString alloc]initWithContentsOfFile:clientCrtPath encoding:NSUTF8StringEncoding error:nil];
//        clientCrtStr = [clientCrtStr stringByReplacingOccurrencesOfString:@"-----BEGIN CERTIFICATE-----" withString:@""];
//        clientCrtStr = [clientCrtStr stringByReplacingOccurrencesOfString:@"-----END CERTIFICATE-----" withString:@""];
        
        
        
//        ZWLog(@" ca = %@ \n  clientPrivate=%@ \n clientCrtStr = %@",ca,clientPrivate,clientCrtStr);
//        NSError *error;
//       BOOL is =  [GRPCCall setTLSPEMRootCerts:ca
//                                withPrivateKey:clientPrivate
//                                 withCertChain:clientCrtStr
//                                       forHost:kHostAddress error:&error];
////      BOOL is =   [GRPCCall setTLSPEMRootCerts:[[NSString alloc]initWithData:clientCrt encoding:NSUTF8StringEncoding] forHost:kHostAddress error:&error];
//        if (is) {
//            ZWLog(@"success   1");
//        }
//        if (error) {
//            ZWLog(@"error 1 = %@",error.userInfo);
//        }
        WeWallet *client = [[WeWallet alloc]initWithHost:kHostAddress];
        wallet = client;
     
    });
    return wallet;
}

static WewalletsdkKeyFactory *factory;
+ (WewalletsdkKeyFactory *)factoryManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        factory = [WewalletsdkKeyFactory new];
    });return factory;
}


//+ (void)creatWellet{
//    WewalletsdkKeyFactory *factory = [WewalletsdkKeyFactory new];
//    WewalletsdkWeWallet *wallet =  [factory generatorWallet];
//    ZWLog(@"私钥 ： %@",wallet.privateKey);
//    ZWLog(@"公钥 ： %@",wallet.publicKey);
//    ZWLog(@"地址 ： %@",wallet.address);
//
//   BOOL is =   [factory verifyAddress:wallet.address];
//    ZWLog(@"地址校验：%d",is);
//   WewalletsdkExecuteResult *address =  [factory importPrivateKey:wallet.privateKey];
//    ZWLog(@"导入后生成的地址%@",address);
//}
//+ (void)


+ (WewalletsdkWeWallet *)createWallet{
    
   return [FACTORY generatorWallet];
}


+ (WewalletsdkExecuteResult *)verifyAddress:(NSString *)address{
    return [FACTORY verifyAddress:address];
}

+ (WewalletsdkWeWallet *)importPrivatekey:(NSString *)privateKey{
    return [FACTORY importPrivateKey:privateKey];
}
//+ (NSString *)exportPrivateKey:(NSString *)addresskey
//{
//    return [ZKeyChain getPrivateForKey:addresskey];
//}
+ (NSString *)getSign:(NSString *)param key:(NSString *)key
{
    WewalletsdkExecuteResult *result = [FACTORY sign:param wifKey:key];
    return result.data;
}
//+ (void)deleteExportPrivate:(NSString *)address{
//    [ZKeyChain deletePrivateForKey:address];
//}
//+ (void)transferFrom:(NSString *)from to:(NSString *)to amount:(NSString *)amount message:(NSString *)message Completion:(void (^)(NSString *, NSInteger))completionhandle
//{
//    NSString *str = [NSString stringWithFormat:@"from=%@&to=%@&amount=%@&currency=%@",from,to,amount,message];
//    
//    NSString *key =  [ZKeyChain getPrivateForKey:from];
//    WewalletsdkWeWallet *wallet = [self importPrivatekey:key];
//    WewalletsdkExecuteResult *result =  [FACTORY sign:str wifKey:key];
//    NSString *url = [NSString stringWithFormat:@"wallet/transfer%@",PortServer];
//    NSDictionary *param = @{
//                            @"from":from,
//                            @"to":to,
//                            @"amount":amount,
//                            @"currency":message,
//                            @"signature":result.data,
//                            @"publicKey":wallet.publicKey,
//                            };
//    
//    ZWLog(@"url = %@\nparam = %@",url,param);
//    [ZNetManager POST:url parameters:param success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
//        ZWLog(@"responseObject = %@",responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
//        ZWLog(@"error = %@",error.userInfo);
//    }];
//    
//    
//}
//+(void)checkPkAddress:(NSString *)address Completion:(void (^)(double, NSString *, NSInteger))completionhandle
//{
//   NSString *url =  [NSString stringWithFormat:@"%@wallet/queryBalance",PortServer];
//    NSDictionary *param = @{@"address":address,
//                            @"currency":@"WE"
//                            };
//    ZWLog(@"url = %@\n param = %@",url,param);
//    [ZNetManager GET:url parameters:param success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
//        NSInteger status = [responseObject[@"status"] integerValue];
//        if (status == 1) {
//            completionhandle([responseObject[@"data"][@"balance"] doubleValue],@"WE",status);
//        }else
//        {
//            [ZProgressManager showError:responseObject[@"message"]];
//        }
//        ZWLog(@"responseObject = %@",responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
//         ZWLog(@"error = %@",error.userInfo);
//    }];
//    NSDictionary *param1 = @{@"address":address,
//                            @"currency":@"CNYT"
//                            };
//    [ZNetManager GET:url parameters:param1 success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
//        NSInteger status = [responseObject[@"status"] integerValue];
//        if (status == 1) {
//            completionhandle([responseObject[@"data"][@"balance"] doubleValue],@"CNYT",status);
//        }else
//        {
//            [ZProgressManager showError:responseObject[@"message"]];
//        }
//        ZWLog(@"responseObject = %@ param1= %@",responseObject,param1);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
//        ZWLog(@"error = %@",error.userInfo);
//    }];
//}


































@end
